<template>
  <div class='home-container'>
    <!-- 导航栏 -->
    <van-nav-bar class="page-nav-bar" fixed>
      <van-button
        class="search-btn"
        slot="title"
        type="info"
        size="small"
        icon="search"
        round>搜索</van-button>
    </van-nav-bar>
    <!-- /导航栏 -->
    <!-- 频道列表 -->
    <van-tabs class="channel-tabs" v-model="active" animated swipeable>
      <van-tab
        :title="channel.name"
        v-for="channel in channels"
        :key="channel.id"
       >
        <article-list :channel="channel" />
       </van-tab>
      <div slot="nav-right" class="placehoder"></div>
      <div slot="nav-right" class="hamburger-btn" @click="isTCshow = true">
        <i class="toutiao icon-gengduo"></i>
      </div>
    </van-tabs>
    <!-- /频道列表-->
    <!-- 频道编辑弹出层 -->
    <van-popup
      v-model="isTCshow"
      position="bottom"
      closeable
      round
      close-icon-position="top-left"
      :style="{ height: '99%' }"
    >
      <channel-edit :my-channels="channels" :active="active" @updated-active="updatedActive" />
    </van-popup>
    <!-- /频道编辑弹出层 -->
  </div>
</template>

<script>
import { getUserChannels } from '@/api/user'
import ArticleList from './components/articlelist'
import ChannelEdit from './components/channel-edit'
import { mapState } from 'vuex'
import { getItem } from '@/utils/storage'
export default {
  name: 'HomeIndex',
  components: {
    ArticleList,
    ChannelEdit
  },
  props: {},
  data () {
    return {
      active: 0,
      channels: [],
      isTCshow: false
    }
  },
  computed: {
    ...mapState(['user'])
  },
  watch: {},
  created () {
    this.loadChannels()
  },
  mounted () {},
  methods: {
    async loadChannels () {
      try {
        let channels = []
        if (this.user) {
          const { data } = await getUserChannels()
          channels = data.data.channels
        } else {
          const localChannles = getItem('TT_CHANNELS')
          if (localChannles) {
            channels = localChannles
          } else {
            const { data } = await getUserChannels()
            channels = data.data.channels
          }
        }

        this.channels = channels
      } catch (err) {
        this.$toast('频道列表请求失败')
      }
    },
    updatedActive (active, isTCshow) {
      this.active = active
      this.isTCshow = isTCshow
    }
  }
}
</script>

<style scoped lang="scss">
.home-container{
  padding-top: 174px;
  padding-bottom: 100px;
  /deep/ .van-nav-bar__title{
     max-width: unset;
  }
  .search-btn{
    width: 555px;
    height: 64px;
    background-color: #5babfb;
    border: none;
    font-size: 28px;
    .van-icon{
      font-size: 32px;
    }
  }
  /deep/ .channel-tabs{
    .van-tabs__wrap{
      height: 82px;
      position: fixed;
      top: 92px;
      z-index: 1;
      left: 0;
      right: 0;
    }
    .van-tab{
      border-right: 1px solid #edeff3;
      min-width: 200px;
      font-size: 30px;
      color: #777;
    }
    .van-tab--active{
      color: #333;
    }
    .van-tabs__nav{
      padding-bottom: 0;
    }
    .hamburger-btn {
      position: fixed;
      right: 0;
      width: 66px;
      height: 82px;
      display: flex;
      justify-content: center;
      align-items: center;
      opacity: 0.902;
      background-color: #fff;
      i.toutiao{
        font-size: 33px;
        color: #000;
      }
      &::before{
        content: '';
        width: 1px;
        height: 100%;
        background-image: url(~@/assets/gradient-gray-line.png);
        position: absolute;
        left: 0;
        background-size: contain;
      }
    }
    .placehoder {
      flex-shrink: 0; //不参与flex计算
      width: 66px;
      height: 82px;
    }
  }
}
</style>
